const path = require('path');
const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');
const app = express();
const port = parseInt(process.argv.find(a => a.startsWith('--port='))?.substring('--port='.length) ?? 3000);
console.log(`listen on port ${port}`);

app.use(function(req,res,next){
    //自定义一下中间件,用来输出请求日志
    console.log(`${req.method} ${req.url}`);
    next();
});

app.use(
    '/api',
    createProxyMiddleware({
        target: 'http://api.github.com',
        changeOrigin: true
    }),
);
//静态文件都在根目录,匹配到dist目录
app.use('/', express.static(path.resolve(__dirname, '../dist')));
app.get('*', function (request, response) {
    //其他默认页或者未知的,都给首页~
    response.sendFile(path.resolve(__dirname, '../dist/index.html'));
});
app.listen(port);